package com.guet.niit.util;

import java.sql.*;

public class JDBCUtil {

    private JDBCUtil(){
    }

    //创建连接
    public static Connection createConnection() throws Exception {
        String url = "jdbc:mysql://localhost:3306/power?useSSL=false&useServerPrepStmts=true&useUnicode=true&characterEncoding=UTF-8";
        String username = "root";
        String password = "root";
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(url, username, password);
        return connection;
    }

    //关闭连接资源
    public static void closeConnection(Connection connection,Statement statement,ResultSet resultSet){
        if (resultSet !=null){
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement !=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
